package root.electronclass.electronclass.personal

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import com.blankj.utilcode.util.LogUtils
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.common.event.PersonSuccess
import root.electronclass.electronclass.common.util.HeadUtil
import root.electronclass.electronclass.mvp.GlobalParameter
import root.electronclass.electronclass.mvp.base.BaseResponse
import root.electronclass.electronclass.mvp.base.CallbackNext
import root.electronclass.electronclass.mvp.entity.UserInfoStudent
import root.electronclass.electronclass.mvp.rest.RestManager
import root.electronclass.electronclass.databinding.FragmentPersonalBinding
import root.electronclass.electronclass.ext.startFragment
import root.electronclass.electronclass.personal.activity.CharacterFragment
import root.electronclass.electronclass.personal.activity.PersonalInfoFragment
import root.electronclass.electronclass.personal.fragment.attendance.PersonalAttendanceFragment
import root.electronclass.electronclass.personal.fragment.pay_record.PersonalPayRecordFragment
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import retrofit2.Call


/**
 */
class PersonalFragment : ViewBindingFragment<FragmentPersonalBinding>() {

    private var data: UserInfoStudent? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)

        }
        onCreate()
    }

    private fun onCreate() { //个人考勤
        binding.selfAttendance.setOnClickListener {
            startFragment(PersonalAttendanceFragment::class.java)
        } //个人德育
        binding.selfMoral.setOnClickListener {
            startFragment(CharacterFragment::class.java)
        } //个人消费
        binding.selfConsumption.setOnClickListener {
            startFragment(PersonalPayRecordFragment::class.java)
        } //个人成绩
        binding.selfCachievement.setOnClickListener {
            startFragment(PersonalPayRecordFragment::class.java)
        } //个人健康
        binding.selfHealthy.setOnClickListener {
            toast("待上线")
        } //个人信息管理
        binding.selfInformation.setOnClickListener {
            if (data == null) {
                toast("数据异常")
                return@setOnClickListener
            }
            startFragment(PersonalInfoFragment::class.java, bundleOf("data" to data))
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun onPersonSuccess(event: PersonSuccess) {
        getData()
    }

    private fun getData() {
        showWaitingMessage()
        RestManager.getInstance().initRest().student().enqueue(object : CallbackNext<BaseResponse<UserInfoStudent>>(mDialogViewModel) {
                    override fun onSuccessful(
                        call: Call<BaseResponse<UserInfoStudent>>?, response: BaseResponse<UserInfoStudent>
                    ) {
                        if (response.isSuccess) {
                            data = response.rows as UserInfoStudent
                            GlobalParameter.setSTUDENT(data)
                            LogUtils.json("学生信息","data:${GlobalParameter.getSTUDENT().toString()}")
                            binding.name.text = data?.name
                            binding.number.text = "卡号：${data?.userNo}"
                            binding.cardNum.text = "一卡通卡号：${data?.canteenBalance?.cardNo}"
                            binding.balance.text = data?.canteenBalance?.balanceString
                            if (data?.userInfoVo?.facePhoto == null || data?.userInfoVo?.facePhotoFormat?.size == 0) {
                                val head = binding.headSdv
                                val pic = HeadUtil.getHeadPic(if (data?.userInfoVo?.gender == 2) 2 else 3)
                                head.setActualImageResource(pic)
                            } else {
                                binding.headSdv.setImageURI(data?.userInfoVo?.facePhotoFormat?.get(0)?.url)
                            }
                        }
                        dismissProgress()
                    }

                    override fun onError(
                        call: Call<BaseResponse<UserInfoStudent>>?, response: BaseResponse<UserInfoStudent>?
                    ) {
                        dismissProgress()
                    }

                })
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}